package cn.new7as.cas5s.ui.apply;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.OnHttpListener;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.interfaces.OnSelectListener;

import java.util.List;

import cn.new7as.cas5s.R;
import cn.new7as.cas5s.ui.easy.netapi.PointApi;
import cn.new7as.cas5s.ui.slideshow.BaseActivity;
import cn.new7as.cas5s.tool.util.Utils;
import cn.new7as.cas5s.databinding.ActivityInfoSecondBinding;
import cn.new7as.cas5s.ui.easy.netapi.GetCusApi;
import cn.new7as.cas5s.ui.easy.netapi.GetListApi;
import cn.new7as.cas5s.ui.easy.netapi.HttpData;
import cn.new7as.cas5s.ui.easy.netapi.GetBaseDataApi;
import cn.new7as.cas5s.ui.easy.netapi.SaveBaseDataApi;

public class Info_SecondActivity extends BaseActivity {
    private String[] rl = new String[]{"Parents"};

    private ActivityInfoSecondBinding binding;

    @SuppressLint("MissingPermission")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityInfoSecondBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setop("Step2", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.go(Info_SecondActivity.this, Info_FirstActivity.class);
                finish();
            }
        });
        show();
        getList();
        getBase();
        initV();


    }

    @Override
    public void initV() {
        super.initV();
        setPoint(binding.p91Ba, 1);
        setPoint(binding.p91Ba2, 2);
        setPoint(binding.p91Ba3, 3);
        setEtListener(binding.nameBa, binding.nameBa2, binding.nameBa3, binding.p91Ba, binding.p91Ba2, binding.p91Ba3);
        binding.rshipBa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showbottom(1);
            }
        });
        binding.rshipBa2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showbottom(2);
            }
        });
        binding.rshipBa3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showbottom(3);
            }
        });
        binding.verifyme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String nameBa = binding.nameBa.getText().toString();
                String p91Ba = binding.p91Ba.getText().toString();
                String nameBa2 = binding.nameBa2.getText().toString();
                String p91Ba2 = binding.p91Ba2.getText().toString();
                String nameBa3 = binding.nameBa3.getText().toString();
                String p91Ba3 = binding.p91Ba3.getText().toString();


                SaveBaseDataApi.Data2 data1 = new SaveBaseDataApi().new Data2();
                SaveBaseDataApi.Data5 data5 = new SaveBaseDataApi().new Data5();
                SaveBaseDataApi.Data5 data6 = new SaveBaseDataApi().new Data5();
                SaveBaseDataApi.Data5 data7 = new SaveBaseDataApi().new Data5();

                data5.fullName = nameBa;
                data5.relationship = sta;
                data5.mobile = p91Ba;

                data6.fullName = nameBa2;
                data6.relationship = sta2;
                data6.mobile = p91Ba2;

                data7.fullName = nameBa3;
                data7.relationship = sta3;
                data7.mobile = p91Ba3;
                data1.reference1 = data5;
                data1.reference2 = data6;
                data1.reference3 = data7;
                show();
                EasyHttp.post(Info_SecondActivity.this)
                        .api(new SaveBaseDataApi(getunique(), data1, 3, null, null, null)).request(new OnHttpListener<HttpData<GetCusApi.CusData>>() {
                            @Override
                            public void onHttpSuccess(@NonNull HttpData<GetCusApi.CusData> result) {

                                GetCusApi.CusData result1 = result.getResult();
                                if (result1.referenceMsg != null) {
                                    setRed(result1.referenceStatus);
                                    toa(result1.referenceMsg);
                                    hide();
                                } else {
                                    point(getunique());
                                }


                            }

                            @Override
                            public void onHttpFail(@NonNull Throwable throwable) {
                                deal(throwable);

                            }
                        });
            }


        });
    }


    long editstart;
    int phonecount;
    int phoneDuration;
    int phone2count;
    int phone2Duration;
    int phone3count;
    int phone3Duration;


    private int ba;

    @Override
    protected void onStop() {
        super.onStop();
        ba++;
    }


    void setPoint(EditText editText, int type) {
        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    switch (type) {
                        case 1:
                            phonecount++;
                            break;
                        case 2:
                            phone2count++;
                            break;
                        case 3:
                            phone3count++;
                            break;

                    }

                    editstart = System.currentTimeMillis();
                } else {
                    int i = (int) ((System.currentTimeMillis() - editstart) / 1000);
                    switch (type) {
                        case 1:
                            phoneDuration += i;
                            break;
                        case 2:
                            phone2Duration += i;
                            break;
                        case 3:
                            phone3Duration += i;
                            break;

                    }

                }
            }
        });
    }

    private void point(String s) {
        long end = System.currentTimeMillis();
        PointApi.Point3Data pointData = new PointApi().new Point3Data();
        pointData.type = 3;

        pointData.basicpage_backend_num = ba;

        pointData.emergencyphone_one_updatecount = phonecount;
        pointData.emergencyphone_one_duration = phoneDuration;

        pointData.emergencyphone_two_updatecount = phone2count;
        pointData.emergencyphone_two_duration = phone2Duration;

        pointData.emergencyphone_three_updatecount = phone3count;
        pointData.emergencyphone_three_duration = phone3Duration;

        pointData.referencepage_stop_duration = (int) ((end - start) / 1000);
        pointData.frist_click_time = Utils.getdate(start);
        pointData.end_click_time = Utils.getdate(end);
        EasyHttp.post(this)
                .api(new PointApi(s, pointData)).request(new OnHttpListener<HttpData<Object>>() {
                    @Override
                    public void onHttpSuccess(@NonNull HttpData<Object> result) {

                        hide();
                        Utils.go(Info_SecondActivity.this, Info_ThirdActivity.class);
                        finish();
                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        deal(throwable);
                    }
                });

    }


    List<GetListApi.ListData> listData;

    void getList() {
        EasyHttp.get(Info_SecondActivity.this)
                .api(new GetListApi(getunique())).request(new OnHttpListener<HttpData<List<GetListApi.ListData>>>() {
                    @Override
                    public void onHttpSuccess(@NonNull HttpData<List<GetListApi.ListData>> result) {
                        hide();
                        if (result.getResult() != null) {
                            listData = result.getResult();
                            rl = new String[listData.size()];
                            for (int i = 0; i < rl.length; i++) {
                                rl[i] = listData.get(i).dicKey;
                            }
                            binding.rshipBa.setText(rl[0]);
                            binding.rshipBa2.setText(rl[1]);
                            binding.rshipBa3.setText(rl[2]);
                         sta=Integer.parseInt(listData.get(0).dicValue)   ;
                         sta2=Integer.parseInt(listData.get(1).dicValue)   ;
                         sta3=Integer.parseInt(listData.get(2).dicValue)   ;
                        }

                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        deal(throwable);

                    }
                });
    }

    void getBase() {
        EasyHttp.post(Info_SecondActivity.this)
                .api(new GetBaseDataApi(getunique(), 3)).request(new OnHttpListener<HttpData<GetBaseDataApi.Data>>() {
                    @Override
                    public void onHttpSuccess(@NonNull HttpData<GetBaseDataApi.Data> result) {
                        hide();
                        if (result.getResult() != null) {
                            try {
                                if (result.getResult().reference1 != null) {
                                    binding.nameBa.setText(result.getResult().reference1.fullName);
                                    binding.p91Ba.setText(result.getResult().reference1.mobile);
                                    binding.rshipBa.setText(rl[result.getResult().reference1.relationship - 1]);
                                    sta = result.getResult().reference1.relationship;
                                }
                                if (result.getResult().reference2 != null) {
                                    binding.nameBa2.setText(result.getResult().reference2.fullName);
                                    binding.p91Ba2.setText(result.getResult().reference2.mobile);
                                    binding.rshipBa2.setText(rl[result.getResult().reference2.relationship - 1]);
                                    sta2 = result.getResult().reference2.relationship;
                                }
                                if (result.getResult().reference3 != null) {
                                    binding.nameBa3.setText(result.getResult().reference3.fullName);
                                    binding.p91Ba3.setText(result.getResult().reference3.mobile);
                                    binding.rshipBa3.setText(rl[result.getResult().reference3.relationship - 1]);
                                    sta3 = result.getResult().reference3.relationship;
                                }
                            } catch (Exception exception) {

                            }

                            setTrue();
                        }
                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        deal(throwable);

                    }
                });
    }

    @Override
    public void setFalse() {
        super.setFalse();
        binding.verifyme.setEnabled(false);
    }

    int sta = 0;
    int sta2 = 1;
    int sta3 = 2;

    @Override
    public void setTrue() {
        super.setTrue();
        serNor();
        if (Utils.isStr(binding.nameBa, binding.nameBa2, binding.nameBa3, binding.p91Ba, binding.p91Ba2, binding.p91Ba3) && Utils.isInt(sta, sta2, sta3))
            binding.verifyme.setEnabled(true);
    }

    void setRed(int type) {
        if (type == 1)
            binding.p91Ba.setTextColor(getResources().getColor(R.color.red));
        else if (type == 2)
            binding.p91Ba2.setTextColor(getResources().getColor(R.color.red));
        else if (type == 3)
            binding.p91Ba3.setTextColor(getResources().getColor(R.color.red));
    }

    void serNor() {
        binding.p91Ba.setTextColor(getResources().getColor(R.color.textblack));
        binding.p91Ba2.setTextColor(getResources().getColor(R.color.textblack));
        binding.p91Ba3.setTextColor(getResources().getColor(R.color.textblack));
    }

    void showbottom(int type) {
        editstart = System.currentTimeMillis();
        BasePopupView s = new XPopup.Builder(this)
                .isDestroyOnDismiss(true)

                .asBottomList("Contact Relationship", rl,
                        null, -1,
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                switch (type) {
                                    case 1:
                                        binding.rshipBa.setText(text);
                                        sta = Integer.parseInt(listData.get(position).dicValue);
                                        break;
                                    case 2:
                                        binding.rshipBa2.setText(text);
                                        sta2 = Integer.parseInt(listData.get(position).dicValue);
                                        break;
                                    case 3:
                                        binding.rshipBa3.setText(text);
                                        sta3 = Integer.parseInt(listData.get(position).dicValue);
                                        break;
                                }
                                setTrue();

                            }
                        })
                .show();
        s.getLifecycle().addObserver(new LifecycleObserver() {
            @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
            public void ondismiss() {
                //  astateDuration += (int) ((System.currentTimeMillis() - editstart) / 1000);

            }
        });
    }

}